package com.ctim.dao.user;

import com.ctim.model.user.Role;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: lb on 2016/8/26.
 * Date:2016-08-26-10:44
 * desc：获取用户角色
 */
@Repository
public class RoleDao {
    @Autowired
    @Qualifier("sqlSession")
    private SqlSessionTemplate sqlSession;


    public List<Role> selectRole(Integer userId) {
        return sqlSession.selectList("dta.role.selectRoleByUserId",userId);
    }

    public List<Role> selectRoleList(String name, Integer pageOffset, Integer pageSize){
        Map<String,Object> map = new HashMap<>();
        map.put("name",name);
        map.put("pageOffset",pageOffset);
        map.put("pageSize",pageSize);
        return  sqlSession.selectList("dta.role.selectRoleList",map);
    }

    public Integer countRole(String name){
        Map<String,Object> map = new HashMap<>();
        map.put("name",name);
        return sqlSession.selectOne("dta.role.countRole", map);
    }

    public void insertRole(Role role) {
        sqlSession.insert("dta.role.insertRole",role);
    }

    public int deleteRole(Integer roleId, Integer updUid){
        Map<String, Integer> map = new HashMap<>();
        map.put("roleId", roleId);
        map.put("updUid", updUid);
        return sqlSession.delete("dta.role.deleteRole",map);
    }

    public Role selectSingleRole(String roleName){
       return sqlSession.selectOne("dta.role.selectSingleRole",roleName);
    }

    public void updateRole(Role role){
        sqlSession.update("dta.role.updateRole",role);
    }

    /**
     * 查询角色下用户数量
     * @param roleId
     * @return
     */
    public int countUser(Integer roleId) {
        return sqlSession.selectOne("dta.role.countUser", roleId);
    }
}